'''
Created on 29/09/2012

@author: Barrios - Correa - Felippone
'''

import os.path as fileMan 


class FileManager():

    def read_file(self, filePath):
        '''
        Read file using the contents of the parameter: fileName.
        Parameters:
        -filePath: path of the file to read from.
        -returns a list with:
          1. Operation result
          2. Operation details. If the file is correct, this position contains the data read from the file
        '''
        result = ['', '']
        existe = fileMan.exists(filePath)
        if existe <> False:
            esArchivo = fileMan.isfile(filePath)
            code = open(filePath, "r")
            if esArchivo <> False: 
                text = code.read()
                result[0] = 'OK'
                result[1] = text
            else:
                result[0] = 'ER'
                result[1] = 'Error al obtener los datos. La ruta especificada no es un archivo. \nRuta: %s' %(filePath)
        else:
            result[0] = 'ER'
            result[1] = 'Error al obtener los datos. No existe archivo en ruta especificada. \nRuta: %s' %(filePath)
        return result

    def write_Py(self, data, fileName):
        '''
        Write a python file using the contents of the parameter: data.
        Parameters:
        -data: the data of the file.
        -fileName: name of the file to write.
        '''
        result = ['', '']
        if(data == ''):
            result[0] += 'inf'
            result[1] += 'The data parameter is empty'
        else:
            fileToWrite = open(fileName + '.py', "w")
            fileToWrite.write(data)
            fileToWrite.close()
            result[0] += 'ok'
            result[1] += 'The file has been written'
        return result
    
    def write_file_exception(self, data, fileName):
        '''
        Write a er file using the contents of the parameter: data.
        Parameters:
        -data: the data of the file.
        -fileName: name of the file to write extension .er.
        '''
        result = ['', '']
        if(data == ''):
            result[0] += 'inf'
            result[1] += 'The data parameter is empty'
        else:
            oFile = open(fileName + '.er', "w")
            oFile.write(data)
            oFile.close()
            result[0] += 'ok'
            result[1] += 'The file has been written'
        return result
    
    def validar_extension(self, path):
        print "Validando extension de archivo."
        ruta = path.split(".")
        index = len(ruta)
        extension = ruta[index - 1]
        return extension == "langue" 
